﻿using CPPEI.WebApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using ENT = CPPEI.ETL.Domain.Entities;
using MOD = CPPEI.ETL.Domain.Models;
using SER = CPPEI.ETL.Domain.Services;
using DAT = CPPEI.ETL.Domain.Data;

namespace CPPEI.ETL.WebApi.DataVerificationRule
{
    /// <summary>
    /// 获取数据审核规则详细信息
    /// </summary>
    [CPPEI.WebApi.AuthGetAction("DVR", "Get", "1.0")]
    public class GetAction : BaseAction<GetParams, GetResult>
    {
        protected override ActionResult<GetResult> OnExecute(ActionContext context,
            SysParameters sysParameters,
            GetParams opParams)
        {
            SER.DataVerificationRuleService service = new SER.DataVerificationRuleService();
            ServiceResult<DAT.DataVerificationRule> serResult = service.Get(opParams.Id);
            if (serResult.IsSuccess)
            {
                GetResult result = new GetResult();
                result.Id = serResult.Data.Rule.Id;

                if (!string.IsNullOrEmpty(serResult.Data.Rule.Caption))
                {
                    result.Caption = serResult.Data.Rule.Caption;
                }
                else
                {
                    result.Caption = string.Empty;
                }

                result.Condition = serResult.Data.Rule.Condition;
                result.Type = serResult.Data.Rule.Type;
                result.CreatedTime = serResult.Data.Rule.CreatedTime;

                for (int index = 0; index < serResult.Data.Parameters.Count; index++)
                {
                    GetResult.Parameter param = new GetResult.Parameter();
                    param.Name = serResult.Data.Parameters[index].Name;
                    param.Value = serResult.Data.Parameters[index].Value;
                    result.Parameters.Add(param);
                }

                return new ActionResult<GetResult>(result);
            }
            else
            {
                return new ActionResult<GetResult>(serResult.IsSuccess, serResult.ErrorCode, serResult.Message, null);
            }
        }
    }

    public class GetParams
    {
        public Guid Id { get; set; }
    }

    public class GetResult
    {
        public Guid Id { get; set; }

        public string Condition { get; set; }

        public string Caption { get; set; }

        /// <summary>
        /// 规则类型，1脚本，2插件,3存储过程
        /// </summary>
        public int Type { get; set; }

        public DateTime CreatedTime { get; set; }

        public List<Parameter> Parameters = new List<Parameter>();

        public class Parameter
        {
            public string Name { get; set; }

            public string Value { get; set; }
        }
    }
}
